<template>
  <div>
    <el-row>
      <el-col :span="4">
        <el-button type="success" @click="addr">+新增</el-button> </el-col
      ><el-col :span="3">
        <el-input
          placeholder="输入关键字或电话号"
          v-model="key"
          clearable
          autocomplete="off"
          size="medium"
          @clear="getList"
        ></el-input>
      </el-col>
      <el-col :span="2" offset="1">
        <el-button
          icon="el-icon-search"
          type="info"
          circle
          @click="getList"
        ></el-button>
      </el-col>
    </el-row>
    <el-table :data="supplier" style="width: 100%">
      <el-table-column prop="name" label="供应商名字"> </el-table-column>
      <el-table-column prop="phone" label="电话号码"> </el-table-column>
      <el-table-column prop="avatar" label="照片">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" width="80" />
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="warning" @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增弹框 -->
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible" width="450px">
      <el-form :model="theSupplier">
        <el-form-item
          label="供应商名字"
          :label-width="formLabelWidth"
          min-width="250px"
        >
          <el-input v-model="theSupplier.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="联系方式"
          :label-width="formLabelWidth"
          min-width="250px"
        >
          <el-input v-model="theSupplier.phone" autocomplete="off"></el-input>
        </el-form-item>
        <div>
          <span>上传图片</span>
          <el-upload
            class="avatar-uploader"
            action="http://localhost:11143/common/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            clearable
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具 js，第三方插件 js，json
import http from "../utils/request";

//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import 引入的组件需要注入到对象中才能使用
  components: {},
  props: {},
  data() {
    //这里存放数据
    return {
      imageUrl: "",
      theSupplier: {
        name: "",
        phone: "",
        avatar: "",
      },
      dialogFormVisible: false,
      supplier: [],
      page: 1,
      limit: 10,
      key: "",
    };
  },
  //计算属性 类似于 data 概念
  computed: {},
  //监控 data 中的数据变化
  watch: {},
  //方法集合
  methods: {
    getSupplier(id) {
      http.get(`/supplier/get/${id}`).then(({ data }) => {
        if (data.code == 1) {
          this.$message({
            type: "success",
            message: "获取成功!",
          });
          this.theSupplier.id = data.data.id;
          this.theSupplier.name = data.data.name;
          this.theSupplier.phone = data.data.phone;
          this.theSupplier.avatar = data.data.avatar;
        }
      });
    },
    submit() {
      if (this.saveOrUpdate == 0) {

          this.submitAdd();
      } else {
        this.Edit();
      }
    },
    handleDelete(data) {
      console.info("选中的删除栏目", data);
      http.delete(`/supplier/delete/${data.id}`).then(({ data }) => {
        if (data.code == 1) {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getList();
        } else {
          this.$message.error("出现了错误!");
        }
      });
    },
    handleEdit(data) {
      this.dialogFormVisible = true;
      this.saveOrUpdate = 1;
      console.info("选中编辑栏目", data);
      this.getSupplier(data.id);
    },
    Edit() {
      this.theSupplier.avatar = this.imageUrl;
      http.put(`/supplier/update`, this.theSupplier).then(({ data }) => {
        if (data.code == 1) {
          this.$message({
            type: "success",
            message: "修改成功!",
          });
          this.getList();
          this.dialogFormVisible = false;
          this.theSupplier = {
            name: "",
            phone: "",
            avatar: "",
          };
        } else {
          this.$message.error("出现了错误!");
        }
      });
    },
    submitAdd() {
      this.theSupplier.avatar = this.imageUrl;
      http.post("/supplier/save", this.theSupplier).then(({ data }) => {
        console.info("返回数据", data);
        if (data.code == 1) {
          //这里加一个提示框
          this.$message({
            type: "success",
            message: "保存成功!",
          });
          console.info("保存成功");
          this.getList();
          this.dialogFormVisible = false;
          this.theSupplier = {
            name: "",
            phone: "",
            avatar: "",
          };
        }
      });
    },
    addr() {
      this.theSupplier = {
          name: "",
          phone: "",
          avatar: "",
        },
      this.dialogFormVisible = true;
      this.saveOrUpdate = 0;
    },
    handleAvatarSuccess(res, file) {
      console.log("返回的链接是", res.data);
      this.imageUrl = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
    },
    getList() {
      http
        .get(
          `/supplier/list?page=${this.page}&limit=${this.limit}&key=${this.key}`
        )
        .then(({ data }) => {
          console.info("接收到传入数据", data.data);
          this.supplier = data.data.records;
          console.info("接收到解析数据", this.supplier);
        });
    },
  },
  //生命周期 - 创建完成（可以访问当前 this 实例）
  created() {
    this.getList();
  },
  //生命周期 - 挂载完成（可以访问 DOM 元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有 keep-alive 缓存功能，这个函数会触发
};
</script>
<style lang="less" scoped>
//@import url(); 引入公共 css 类
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
